﻿using Batman.Core;
using IdentityServer4;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Mvc;
using Microsoft.Extensions.DependencyInjection;
using System.Linq;
using System.Threading.Tasks;
using IdentityModel.Client;
using Microsoft.Extensions.Configuration;

namespace Batman.Web.Api.Controllers
{
    [Route("api/[Controller]")]
    [ApiController]
    [Authorize(IdentityServerConstants.LocalApi.PolicyName)]
    public class AllowedGrantTypesController : ControllerBase
    {
        IServiceScopeFactory _serviceScopeFactory;

        IConfiguration _configuration;

        public AllowedGrantTypesController(IServiceScopeFactory serviceScopeFactory, IConfiguration configuration)
        {
            _serviceScopeFactory = serviceScopeFactory;
            _configuration = configuration;
        }

        [HttpGet]
        public async Task<ResponseBase> Get()
        {
            var cache = new DiscoveryCache(_configuration["ID4Server:Address"]);
            var disco = await cache.GetAsync();
            var discoGrantTypesSupported = disco.GrantTypesSupported.ToList();

            return new ResponseBase(discoGrantTypesSupported);
        }
    }
}
